package com.sc.service.goods;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sc.dal.mapper.consultComment.ConsultCommentMapper;
import com.sc.dal.mapper.goods.GoodsMapper;
import com.sc.dal.mapper.goods.GoodsSkuMapper;
import com.sc.dal.mapper.goods.GoodsStockMapper;
import com.sc.dal.model.consultComment.ConsultComment;
import com.sc.dal.model.favorites.Favorites;
import com.sc.dal.model.goods.Goods;
import com.sc.dal.model.goods.GoodsSku;
import com.sc.dal.model.goods.GoodsStock;
import com.sc.dal.model.order.Products;
import com.sc.service.BaseServiceImpl;
import com.sc.service.member.MemberService;

/**
 * 商品服务实现类
 * @author maoyongbin
 * @日期 Mar 15, 2017 9:06:53 AM
 */
@Service
public class GoodsService extends BaseServiceImpl<Map<String, Object>>{
	@Autowired
	private GoodsMapper goodsMapper;
	// 商品规格信息
	@Autowired
	private GoodsSkuMapper goodsSkuMapper;
	@Autowired
	private GoodsStockMapper goodsStockMapper;
	//浏览足迹信息
	@Autowired
	private ConsultCommentMapper consultCommentMapper;

    @Autowired
    private MemberService  memberService;
  
	
	/**
	 * 保存商品
	 * @param goods
	 * @return int
	 */
	@Transactional
	public int insert(Goods goods){
		//处理商品相册图片
		String[] photos = goods.getPhotos();
		if(photos!=null && photos.length>0){
			StringBuilder photoSb = new StringBuilder();
			for(String photo : photos){
				photoSb.append(photo).append(";");
			}
			goods.setImageFile(photoSb.toString().substring(0, photoSb.length()-1));
		}
		
		//处理商品类型
		String[] types = goods.getTypes();
		if(types!=null && types.length>0){
			StringBuilder typeSb = new StringBuilder();
			for(String type : types){
				typeSb.append(type).append("-");
			}
			
			goods.setType(typeSb.toString().substring(0, typeSb.length()-1));
		}
		
		int resultInt = this.goodsMapper.insert(goods);
		
		//保存商品对应的库存信息
		if("1".equals(goods.getIsOpenSku())){
			List<GoodsStock> goodsStockList = goods.getGoodsStockList();
			if(goodsStockList!=null && goodsStockList.size()>0){	//多规格商品
				for(GoodsStock goodsStock : goodsStockList){
					if(goodsStock.getId()==-1){
						goodsStock.setId(null);
					}
					String[] skuValueArray = goodsStock.getSkuValue().split("、");
					//根据规格生成的货品来设置数据
					for(int i=0; i<skuValueArray.length; i++){	
						String[] skuValue = skuValueArray[i].split("：");
						if(i==0){
							goodsStock.setFreeItem0(skuValue[1]);
							continue;
						}
						if(i==1){
							goodsStock.setFreeItem1(skuValue[1]);
							continue;
						}
						if(i==2){
							goodsStock.setFreeItem2(skuValue[1]);
							continue;
						}
						if(i==3){
							goodsStock.setFreeItem3(skuValue[1]);
							continue;
						}
						if(i==4){
							goodsStock.setFreeItem4(skuValue[1]);
							continue;
						}
					}
					
					goodsStock.setGoodsId(goods.getId());
					goodsStock.setModifyTime(goods.getModifyTime());
					goodsStock.setCreateTime(goods.getModifyTime());
					this.goodsStockMapper.insert(goodsStock);	//新增
					
					//保存商品属性规格
					if(StringUtils.isNotEmpty(goodsStock.getSkuValueId())){
						String[] skuIds = goodsStock.getSkuValueId().split("、");
						String[] skuValues = goodsStock.getSkuValue().split("、");
						String[] skuReleImgs = goodsStock.getSkuReleImg().split("、");
						for(int i=0; i<skuIds.length; i++){
							String skuIdString = skuIds[i];
							String skuValueString = skuValues[i];
							String skuReleImg = "";
							if(i<skuReleImgs.length){
								skuReleImg = skuReleImgs[i];
							}
							String[] skuId = skuIdString.split(":");
							String[] skuValue = skuValueString.split("：");
							
							GoodsSku goodsSku = new GoodsSku();
							goodsSku.setBaseSkuId(Integer.valueOf(skuId[0]));	//设置规格id
							goodsSku.setBaseSkuValueId(Integer.valueOf(skuId[1]));	//设置规格值id
							goodsSku.setName(skuValue[1]);	//设置规格值
							goodsSku.setGoodsId(goods.getId());
							goodsSku.setSkuReleImg(skuReleImg);
							goodsSku.setCreater(goods.getModifier());
							goodsSku.setCreateTime(goods.getModifyTime());
							goodsSku.setModifier(goods.getModifier());
							goodsSku.setModifyTime(goods.getModifyTime());
							goodsSku.setIsDelete("0");
							
							//先判断是否已经有相应的规格了
							GoodsSku goodsSkuOld = this.goodsSkuMapper.selectOneByParam(goodsSku);
							if(goodsSkuOld==null){
								this.goodsSkuMapper.insert(goodsSku);	//新增
							}
						}
					}
				}
			}
		}else{	//单规格商品，需要同时在货品表中保存一条记录
			GoodsStock goodsStock = new GoodsStock();
			goodsStock.setGoodsId(goods.getId());
			goodsStock.setCreateTime(goods.getCreateTime());
			goodsStock.setModifyTime(goods.getModifyTime());
			if(StringUtils.isNotBlank(goods.getBaseQuantity())){
				goodsStock.setBaseQuantity(Integer.valueOf(goods.getBaseQuantity()));
			}
			
			this.goodsStockMapper.insert(goodsStock);	//新增
		}
		
		return resultInt;
	}
	
	/**
	 * 更新商品
	 * @param goods
	 * @return int
	 */
	@Transactional
	public int update(Goods goods){
		//处理商品相册图片
		String[] photos = goods.getPhotos();
		if(photos!=null && photos.length>0){
			StringBuilder photoSb = new StringBuilder();
			for(String photo : photos){
				photoSb.append(photo).append(";");
			}
			
			goods.setImageFile(photoSb.toString().substring(0, photoSb.length()-1));
		}
		
		//处理商品类型
		String[] types = goods.getTypes();
		if(types!=null && types.length>0){
			StringBuilder typeSb = new StringBuilder();
			for(String type : types){
				typeSb.append(type).append("-");
			}
			
			goods.setType(typeSb.toString().substring(0, typeSb.length()-1));
		}
		
		int resultInt = this.goodsMapper.updateByPrimaryKey(goods);
		
		//修改商品对应的库存信息
		if("1".equals(goods.getIsOpenSku())){
			List<GoodsStock> goodsStockList = goods.getGoodsStockList();
			if(goodsStockList!=null && goodsStockList.size()>0){	//多规格商品
				for(GoodsStock goodsStock : goodsStockList){
					goodsStock.setModifyTime(goods.getModifyTime());
					this.goodsStockMapper.updateByPrimaryKey(goodsStock);	//更新
				}
			}
			
		}else{	//单规格商品，需要同时在货品表中保存一条记录
			GoodsStock goodsStock = new GoodsStock();
			goodsStock.setGoodsId(goods.getId());
			goodsStock.setModifyTime(goods.getModifyTime());
			goodsStock.setBaseQuantity(Integer.valueOf(goods.getBaseQuantity()));
			
			this.goodsStockMapper.updateByGoodsId(goodsStock);	//更新
		}
		
		return resultInt;
	}
	
	/**
	 * 删除商品
	 * @param goods
	 * @return int
	 */
	public int delete(Goods goods){
		int resultInt = this.goodsMapper.updateByPrimaryKey(goods);
		
		return resultInt;
	}
	
	/**
	 * 查找商品详情
	 * @param id
	 * @return Goods
	 */
	public Goods selectOne(Integer id){
		Goods goods = this.goodsMapper.selectByPrimaryKey(id);
		
		return goods;
	}
	
	/**
	 * 查找商品详情
	 * @param id
	 * @return Goods
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Goods selectByPrimaryKey(Integer id,String mId){
		Map map = new LinkedHashMap<String, Object>();
		map.put("id", id);
		map.put("mId", mId);
		Goods goods = this.goodsMapper.selectByGoods(map);
		/*if(goods!=null){
			List<GoodsSku> GoodsSkuList = this.goodsSkuMapper.selectbyGoodsSkuList(id);
			if(GoodsSkuList!=null && GoodsSkuList.size()>0){
				goods.setGoodsSkuList(GoodsSkuList);
				GoodsSku record = new GoodsSku();
				for (GoodsSku goodsSku : GoodsSkuList) {
					Integer baseSkuId = goodsSku.getBaseSkuId();
					record.setBaseSkuId(baseSkuId);
					record.setGoodsId(id);
					List<GoodsSku> goodsSkuValueList = this.goodsSkuMapper.selectbyGoodsSkuValueList(record);
					if(goodsSkuValueList!=null && goodsSkuValueList.size()>0){
						goodsSku.setGoodsSkuValueList(goodsSkuValueList);
					}
				}
			}
		}
		*/
		/**当登录人登录后记录足迹信息******/
		if(mId!=null && !"".equals(mId)){
			ConsultComment record = new ConsultComment();
			record.setGoodsId(id);//商品ID
			record.setType("3"); //类型
			record.setCreater(mId); //创建人  //记录会员ID信息 2017.5.26变更，之前记录会员名称。
			record.setCreateTime(new Date());
			record.setModifyTime(new Date());
			Map<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("goodsId", id.toString());
			hashMap.put("type", "3");
			hashMap.put("accountName", mId);
			List<ConsultComment> list = consultCommentMapper.selectByBoolean(hashMap);
			if(list!=null && list.size()>0){
				for (ConsultComment consultComment : list) {
					consultCommentMapper.deleteByPrimaryKey(consultComment.getId());
				}
			}
			consultCommentMapper.insertSelective(record);
		}
		
		return goods;
	}
	
	//根据id查找商品
	public Goods selectByPrimaryKey(Integer id){
		return goodsMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据参数查找商品
	 * @param paramMap
	 * @return List<Goods>
	 */
	public List<Goods> selectList(Map<String, Object> paramMap){
		List<Goods> goodsList = this.goodsMapper.selectList(paramMap);
		
		return goodsList;		
	}	
	public GoodsStock findGoodsStockByParam(GoodsStock stock){
	    return goodsStockMapper.findGoodsStockByParam(stock);	
	}

	//封装商品库存对象
	public GoodsStock getGoodsStock(GoodsStock stock, String[] skuInfoArrs){
		if(skuInfoArrs != null && skuInfoArrs.length > 0){

			
			int skuSize = skuInfoArrs.length;
			String[] skuInfoValuesArrs0={};
			String[] skuInfoValuesArrs1={};
			String[] skuInfoValuesArrs2={};
			String[] skuInfoValuesArrs3={};
			String[] skuInfoValuesArrs4={};
			String[] skuInfoValuesArrs5={};
			String[] skuInfoValuesArrs6={};
			String[] skuInfoValuesArrs7={};
			String[] skuInfoValuesArrs8={};
			String[] skuInfoValuesArrs9={};
			switch (skuSize) {
			case 1:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				break;
			case 2:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				break;
			case 3:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				break;
			case 4:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				break;
			case 5:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				skuInfoValuesArrs4 =skuInfoArrs[4].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				if(skuInfoValuesArrs4.length > 1 ){
					stock.setFreeItem4(skuInfoValuesArrs4[1]);
				}
				break;
			case 6:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				skuInfoValuesArrs4 =skuInfoArrs[4].split(":");
				skuInfoValuesArrs5 =skuInfoArrs[5].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				if(skuInfoValuesArrs4.length > 1 ){
					stock.setFreeItem4(skuInfoValuesArrs4[1]);
				}
				if(skuInfoValuesArrs5.length > 1 ){
					stock.setFreeItem5(skuInfoValuesArrs5[1]);
				}
				break;
			case 7:
				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				skuInfoValuesArrs4 =skuInfoArrs[4].split(":");
				skuInfoValuesArrs5 =skuInfoArrs[5].split(":");
				skuInfoValuesArrs6 =skuInfoArrs[6].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				if(skuInfoValuesArrs4.length > 1 ){
					stock.setFreeItem4(skuInfoValuesArrs4[1]);
				}
				if(skuInfoValuesArrs5.length > 1 ){
					stock.setFreeItem5(skuInfoValuesArrs5[1]);
				}
				if(skuInfoValuesArrs6.length > 1 ){
					stock.setFreeItem6(skuInfoValuesArrs6[1]);
				}
				break;
			case 8:

				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				skuInfoValuesArrs4 =skuInfoArrs[4].split(":");
				skuInfoValuesArrs5 =skuInfoArrs[5].split(":");
				skuInfoValuesArrs6 =skuInfoArrs[6].split(":");
				skuInfoValuesArrs7 =skuInfoArrs[7].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				if(skuInfoValuesArrs4.length > 1 ){
					stock.setFreeItem4(skuInfoValuesArrs4[1]);
				}
				if(skuInfoValuesArrs5.length > 1 ){
					stock.setFreeItem5(skuInfoValuesArrs5[1]);
				}
				if(skuInfoValuesArrs6.length > 1 ){
					stock.setFreeItem6(skuInfoValuesArrs6[1]);
				}
				if(skuInfoValuesArrs7.length > 1 ){
					stock.setFreeItem7(skuInfoValuesArrs7[1]);
				}
				break;
			case 9:

				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				skuInfoValuesArrs4 =skuInfoArrs[4].split(":");
				skuInfoValuesArrs5 =skuInfoArrs[5].split(":");
				skuInfoValuesArrs6 =skuInfoArrs[6].split(":");
				skuInfoValuesArrs7 =skuInfoArrs[7].split(":");
				skuInfoValuesArrs8 =skuInfoArrs[8].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				if(skuInfoValuesArrs4.length > 1 ){
					stock.setFreeItem4(skuInfoValuesArrs4[1]);
				}
				if(skuInfoValuesArrs5.length > 1 ){
					stock.setFreeItem5(skuInfoValuesArrs5[1]);
				}
				if(skuInfoValuesArrs6.length > 1 ){
					stock.setFreeItem6(skuInfoValuesArrs6[1]);
				}
				if(skuInfoValuesArrs7.length > 1 ){
					stock.setFreeItem7(skuInfoValuesArrs7[1]);
				}
				if(skuInfoValuesArrs8.length > 1 ){
					stock.setFreeItem8(skuInfoValuesArrs8[1]);
				}
				break;
			case 10:

				skuInfoValuesArrs0 =skuInfoArrs[0].split(":");
				skuInfoValuesArrs1 =skuInfoArrs[1].split(":");
				skuInfoValuesArrs2 =skuInfoArrs[2].split(":");
				skuInfoValuesArrs3 =skuInfoArrs[3].split(":");
				skuInfoValuesArrs4 =skuInfoArrs[4].split(":");
				skuInfoValuesArrs5 =skuInfoArrs[5].split(":");
				skuInfoValuesArrs6 =skuInfoArrs[6].split(":");
				skuInfoValuesArrs7 =skuInfoArrs[7].split(":");
				skuInfoValuesArrs8 =skuInfoArrs[8].split(":");
				skuInfoValuesArrs9 =skuInfoArrs[9].split(":");
				if(skuInfoValuesArrs0.length > 1 ){
					stock.setFreeItem0(skuInfoValuesArrs0[1]);
				}
				if(skuInfoValuesArrs1.length > 1 ){
					stock.setFreeItem1(skuInfoValuesArrs1[1]);
				}
				if(skuInfoValuesArrs2.length > 1 ){
					stock.setFreeItem2(skuInfoValuesArrs2[1]);
				}
				if(skuInfoValuesArrs3.length > 1 ){
					stock.setFreeItem3(skuInfoValuesArrs3[1]);
				}
				if(skuInfoValuesArrs4.length > 1 ){
					stock.setFreeItem4(skuInfoValuesArrs4[1]);
				}
				if(skuInfoValuesArrs5.length > 1 ){
					stock.setFreeItem5(skuInfoValuesArrs5[1]);
				}
				if(skuInfoValuesArrs6.length > 1 ){
					stock.setFreeItem6(skuInfoValuesArrs6[1]);
				}
				if(skuInfoValuesArrs7.length > 1 ){
					stock.setFreeItem7(skuInfoValuesArrs7[1]);
				}
				if(skuInfoValuesArrs8.length > 1 ){
					stock.setFreeItem8(skuInfoValuesArrs8[1]);
				}
				if(skuInfoValuesArrs9.length > 1 ){
					stock.setFreeItem9(skuInfoValuesArrs9[1]);
				}
				break;
			default:
				break;

			}
		}
		return stock;
	}
	
	/**
	 * 根据商品规格查询对应的规格库存
	 * @param stock
	 * @return
	 */
	public int getProductStock(GoodsStock stock, String[] skuInfoArrs) {
		int productStock = 0;
		// 2017年3月28日暂定商品库存中的规格值项是按t_base_sku中的id顺序有小到大依次填充的
		stock = getGoodsStock(stock,skuInfoArrs);
		Integer baseQuantit = goodsStockMapper.selectBaseQuantity(stock);
		Integer saleBaseQuantity = goodsStockMapper.selectSaleBaseQuantity(stock);
		if (baseQuantit == null || saleBaseQuantity == null) {
			productStock = 0;
		}
		productStock = baseQuantit - saleBaseQuantity;
		return productStock;
	}
	
	//根据商品规格值项修改商品库存
	public int updateGoodsStock(GoodsStock stock) {
		return goodsStockMapper.updateGoodsStock(stock);
	}
	
	/**
     * 查找推荐商品
     * @return List<Goods>
     */
    public List<Goods> selectListRecommend(){
    	List<Goods> goodsList = this.goodsMapper.selectListRecommend();
    	
    	return goodsList;
    }
	/**
	 * 根据商品分类ID查询商品列表
	 * @param map
	 * @return
	 */
    public List<Goods> queryGoodsByClassIdList(Map<String, Object> map){
    	List<Goods> goodsList = this.goodsMapper.queryGoodsByClassIdList(map);
    	return goodsList;
    }
    
    /**
     * 查找单表单个商品
     * @param id
     * @return Goods
     */
    public Goods selectOneGoods(Integer id){
    	Goods goods = this.goodsMapper.selectOneGoods(id);
    	
    	return goods;
    }
    
    /**
     * 新增单表单个商品
     * @param goods
     * @return int
     */
    public int insertOneGoods(Goods goods){
    	int resultInt = this.goodsMapper.insert(goods);
    	
    	return resultInt;
    }
    
    /**
     * 更新单表单个商品
     * @param goods
     * @return int
     */
    public int updateOneGoods(Goods goods){
    	int resultInt = this.goodsMapper.updateByPrimaryKey(goods);
    	return resultInt;
    }
    
    /***
     * 统计商品总数
     * @return
     */
    public List<Integer> findTotal(){
    	return goodsMapper.findTotal();
    }
    
    
    public List<Integer> statisticsGoodsDetails(Integer id){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("goodsId", id);
    	return goodsMapper.statisticsGoodsDetails(map);
    }

    /***
     * 根据id查询相关商品数据
     * @param idMap
     * @return
     */
	public List<Goods> queryGoodsListById(Map<String, Object> idMap) {
		return goodsMapper.queryGoodsListById(idMap);
	}
	
	/**
	 * @title 查询商品的收藏人气 
	 * @param paramMap
	 * @return
	 */
	public List<Favorites> goodsCollectionSentiment(Map<String,Object> paramMap){
		return goodsMapper.goodsCollectionSentiment(paramMap); 
	}
	
	/**
	 * 查询商品的月销数量
	 * @param goodsId
	 * @return
	 */
	public int selectGoodsMonthSaleMount(Integer goodsId){
		return goodsMapper.selectGoodsMonthSaleMount(goodsId);
	}
	

	//获取代理商打折后的商品
	public Products discoutProducts(Products product,String memberId){
		//根据会员的代理商等级查询代理等级的商品折扣率
		Map<String,Object> agentInfoMap = memberService.selectAgentInfoByMemberId(memberId);
		if(agentInfoMap != null && agentInfoMap.size() > 0 ){
			if(agentInfoMap.get("discount") != null){
				double discount = (double)agentInfoMap.get("discount");	
				//product.setMarketPrice(product.getMarketPrice().multiply(new BigDecimal(discount)).setScale(2,  BigDecimal.ROUND_HALF_UP));							   
			    product.setDiscountMarketPrice(product.getMarketPrice().multiply(new BigDecimal(discount)).setScale(2,  BigDecimal.ROUND_HALF_UP));
			    product.setDiscountRate(discount);
			}
			
		}else{
			product.setDiscountMarketPrice(product.getMarketPrice().setScale(2,  BigDecimal.ROUND_HALF_UP));
			product.setDiscountRate(1);
		}
		product.setMarketPrice(product.getMarketPrice().setScale(2,  BigDecimal.ROUND_HALF_UP));
		return product;
	}
	
	/****
	 * 热门词汇关键字
	 * @return
	 */
	public List<String> queryHotWords(){
		return goodsMapper.queryHotWords();
	}
}
